package com.example.administrator.stulist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.zip.Inflater;

public class StuAdapter extends BaseAdapter{
    private ArrayList<Student> students;
    private Context context;
    private IonDelListener listener;
    public StuAdapter(Context context,ArrayList<Student> students,IonDelListener listener){
        this.context=context;
        this.students=students;
        this.listener=listener;
    }
    public  void changedata( ArrayList<Student> students){
        this.students=students;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return students.size();
    }

    @Override
    public Object getItem(int i) {
        return students.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final  int i, View view, ViewGroup viewGroup) {
        if(view==null){
            Holder holder=new Holder();
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        view=layoutInflater.inflate(R.layout.activity_my,viewGroup,false);
        holder.headview=view.findViewById(R.id.headview);
        holder.name=view.findViewById(R.id.name_id);
        holder.age=view.findViewById(R.id.age_id);
        holder.del=view.findViewById(R.id.stu_del);
        view.setTag(holder);
       }
       Holder holder=(Holder) view.getTag();
        holder.headview.setImageResource(students.get(i).getImgid());
        holder.name.setText(students.get(i).getName());
        holder.age.setText(String.valueOf(students.get(i).getAge()));
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.del(i);
            }
        });
        return view;
    }
}
